package com.xc.ksxt.controller;

import com.alibaba.fastjson.JSON;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.LibChoice;
import com.xc.ksxt.entity.LibChoiceEntity;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.service.LibChoiceService;
import com.xc.ksxt.vo.LibChoiceVO;
import com.xc.sys.entity.SysDictEntity;
import com.xc.sys.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/libChoice")
public class LibChoiceController {
    @Autowired
    private DSLContext dsl;
    @Autowired
    private LibChoiceService libChoiceService;
    private LibChoice LIBCHOICETB = LibChoice.LIB_CHOICE.as("LIBCHOICETB");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<LibChoiceVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<LibChoiceVO> ltr = new LayuiTableResponse<>();
        Condition cond = DSL.trueCondition();
        String delFlag = request.getParameter("delFlag");
        String questionId = request.getParameter("questionId");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(questionId)) {
        	cond = cond.and(LIBCHOICETB.LIB_QUESTION_ID.eq( Long.valueOf(questionId) ));
        }
        if (StringUtils.isNotBlank(delFlag)) {
        	cond = cond.and(LIBCHOICETB.DEL_FLAG.eq( Integer.valueOf(delFlag) ));
        }
        Long count = libChoiceService.count(cond);
        ltr.setCount(count);
        PageInfo<LibChoiceEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = libChoiceService.select(cond, pageInfo,LIBCHOICETB.INDEX_LETTER.asc(),LIBCHOICETB.ID.desc());
        List<LibChoiceVO> libChoiceEntityList = list.into(LibChoiceVO.class);
        for (LibChoiceVO libChoiceEntity : libChoiceEntityList) {
            SysDictEntity dict = SysDictService.getFromCaches("y-n," + libChoiceEntity.getRight());
            libChoiceEntity.setRightTxt(dict==null?"":dict.getName());
        }
        ltr.setData(libChoiceEntityList);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public LibChoiceEntity getById(Integer id) {
        LibChoiceEntity entity = libChoiceService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<LibChoiceEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
    	ResponseEntity<LibChoiceEntity> res = new ResponseEntity<>();
    	LibChoiceEntity libChoiceEntity = Helper.map2Bean(new LibChoiceEntity(), params);
        String text = libChoiceEntity.getText();
        if(text.startsWith("<p>")){
            text = text.substring(3);
            int end = text.lastIndexOf("</p>");
            text = text.substring(0,end);
        }
        String dasm = libChoiceEntity.getAnswerText();
        if(dasm.startsWith("<p>")){
            dasm = dasm.substring(3);
            int end = dasm.lastIndexOf("</p>");
            dasm = dasm.substring(0,end);
        }
        libChoiceEntity.setAnswerText(dasm);
        libChoiceEntity.setText(text);
    	libChoiceService.save(libChoiceEntity);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<LibChoiceEntity> delete(@RequestBody Map<String,String> params) {
        LayuiTableResponse<LibChoiceEntity> ltr = new LayuiTableResponse<>();
        String ids = params.get("ids");
        Integer[] idarr = Helper.str2integerArr(ids);
        dsl.fetch("update lib_choice set del_flag=-1 where id in (?)",idarr);
        return ltr;
    }

}
